package com.qianguo.buyer.widget;


import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;


import com.qianguo.buyer.R;

import java.util.List;

/**
 * 列表dialog
 * Created by drakeet on 9/28/14.
 */
public class ListContentDialog extends Dialog {

    private final Builder mBuilder;

    public ListContentDialog(Builder builder) {
        super(builder.mContext, builder.mIsCancel, builder.mOnCancelListener);
        this.mBuilder = builder;
        init();
    }

    void init() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        final LayoutInflater inflater = LayoutInflater.from(mBuilder.mContext);
        View view = inflater.inflate(R.layout.dialog_delivery_time, null);
        setContentView(view);
        TextView title = (TextView) view.findViewById(R.id.dialog_title);
        title.setText(mBuilder.mTitle);
        ListView contentList = (ListView) view.findViewById(R.id.dialog_list_view);
        SimpleListAdapter mAdapter = new SimpleListAdapter(mBuilder.mContext);
        contentList.setAdapter(mAdapter);
        contentList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(mBuilder.mListClickCallback!=null){
                    mBuilder.mListClickCallback.onSelection(position,mBuilder.mContentList.get(position).toString());
                }
                dismiss();
            }
        });

    }

    public static class Builder {
        protected final Context mContext;
        protected String mTitle;//标题
        protected List<String> mContentList;//内容list
        protected ListCallback mListClickCallback;//item 点击callback
        protected boolean mIsCancel = true;//是否允许点击dialog外面消失
        protected OnCancelListener mOnCancelListener;

        public Builder(@NonNull Context context) {
            this.mContext = context;
        }

        public Builder title(@NonNull String title) {
            this.mTitle = title;
            return this;
        }

        public Builder content(@NonNull List<String> content) {
            this.mContentList = content;
            return this;
        }

        public Builder isCancel(@NonNull boolean isCancel) {
            this.mIsCancel = isCancel;
            return this;
        }

        public Builder setListCallback(@NonNull ListCallback listCallback) {
            this.mListClickCallback = listCallback;
            return this;
        }

        public Builder setOnCancelListener(@NonNull OnCancelListener onCancelListener) {
            this.mOnCancelListener = onCancelListener;
            return this;
        }

        public ListContentDialog build() {
            return new ListContentDialog(this);
        }

        public ListContentDialog show() {
            ListContentDialog dialog = build();
            dialog.show();
            return dialog;
        }
    }

    public interface ListCallback {
        void onSelection(int position,String str);
    }

    public class SimpleListAdapter extends BaseAdapter {

        private LayoutInflater mInflater;

        public SimpleListAdapter(Context mCtx) {
            super();
            mInflater = LayoutInflater.from(mCtx);
        }


        @Override
        public int getCount() {
            return mBuilder.mContentList == null ? 0 : mBuilder.mContentList.size();
        }

        @Override
        public String getItem(int position) {
            return mBuilder.mContentList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.item_list_dialog, null);
                holder.tvName = (TextView) convertView.findViewById(R.id.tv_time_value);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            try {
                holder.tvName.setText(String.valueOf(mBuilder.mContentList.get(position)));
            }catch (NumberFormatException e){
                e.printStackTrace();
            }
            return convertView;
        }


    }

    static class ViewHolder {
        public TextView tvName;

    }

}


